package info.thereisonlywe.onenesspathfinder.ui;

import info.thereisonlywe.core.font.FontLibrary;
import info.thereisonlywe.core.project.ProjectConstants;
import info.thereisonlywe.core.project.ProjectLocalization;
import info.thereisonlywe.onenesspathfinder.launcher.Main;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

public class WindowsView extends JPanel {

	private BookmarkPane pane_Bookmark;
	private SearchPanel panel_Search;
	private ScratchPanel panel_Scratch;

	private JPanel container_Search;
	private SearchConfigurationPanel panel_SearchConfiguration;

	private boolean scratchWindowMounted = false;
	private boolean bookmarksWindowMounted = false;
	private boolean searchContainerMounted = false;

	// private final JToggleButton button_ShowArabicKeys = new JToggleButton(
	// ProjectLocalization.getString(
	// ProjectConstants.ONENESS_PATHFINDER_PROJECT,
	// "ShowArabicKeys"));

	// button_ShowArabicKeys.addActionListener(new ActionListener()
	// {
	// @Override
	// public void actionPerformed(ActionEvent e)
	// {
	// panel_ArabicKeys.setVisible(button_ShowArabicKeys.isSelected());
	// MainFrame.getSplitPane().getControlPanel().getMenuPane()
	// .getSearchPanel().requestFocus();
	// }
	// });

	public boolean isScratchWindowMounted()
	{
		return scratchWindowMounted;
	}

	public boolean isBookmarksWindowMounted()
	{
		return bookmarksWindowMounted;
	}

	public boolean isSearchContainerMounted()
	{
		return searchContainerMounted;
	}

	public boolean isAWindowMounted()
	{
		return scratchWindowMounted || bookmarksWindowMounted
				|| searchContainerMounted;
	}

	public WindowsView()
	{
		super();
		initComponents();
	}

	private void initComponents()
	{
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		setPreferredSize(new Dimension(0, 0));
	}

	protected void mountBookmarkPane()
	{
		if (bookmarksWindowMounted) return;
		add(getBookmarkPane());
		MainFrame.view_Navigation.button_Bookmarks.setSelected(true);
		MainFrame.view_Navigation.button_Bookmarks.setContentAreaFilled(true);
		MainFrame.view_Navigation.button_Bookmarks.setBorderPainted(true);
		MainFrame.view_Navigation.button_Bookmarks.setOpaque(true);
		MainFrame.view_Navigation.button_Bookmarks.repaint();
		bookmarksWindowMounted = true;
		resize();
		revalidate();
		UIDelegate.SectionPaneHook.focusRow(UIDelegate.getDisplayedVerse()
				.getVerseNumber() - 1);
	}

	protected void mountScratchPanel()
	{
		if (scratchWindowMounted) return;
		add(getScratchPanel());
		MainFrame.view_Navigation.button_ScratchPanel.setSelected(true);
		MainFrame.view_Navigation.button_ScratchPanel
				.setContentAreaFilled(true);
		MainFrame.view_Navigation.button_ScratchPanel.setBorderPainted(true);
		MainFrame.view_Navigation.button_ScratchPanel.setOpaque(true);
		MainFrame.view_Navigation.button_ScratchPanel.repaint();
		scratchWindowMounted = true;
		resize();
		revalidate();
		getScratchPanel().requestFocus();
		UIDelegate.SectionPaneHook.focusRow(UIDelegate.getDisplayedVerse()
				.getVerseNumber() - 1);

	}

	protected void mountSearchContainer()
	{
		if (searchContainerMounted) return;
		getSearchFilterPanel().refreshValues();
		add(getSearchContainer());
		MainFrame.view_Navigation.button_Search.setSelected(true);
		MainFrame.view_Navigation.button_Search.setContentAreaFilled(true);
		MainFrame.view_Navigation.button_Search.setBorderPainted(true);
		MainFrame.view_Navigation.button_Search.setOpaque(true);
		MainFrame.view_Navigation.button_Search.repaint();
		searchContainerMounted = true;
		resize();
		revalidate();
		getSearchPanel().requestFocus();
		UIDelegate.SectionPaneHook.focusRow(UIDelegate.getDisplayedVerse()
				.getVerseNumber() - 1);
	}

	protected void unmountScratchPanel()
	{
		if (!scratchWindowMounted) return;
		remove(getScratchPanel());
		MainFrame.view_Navigation.button_ScratchPanel.setSelected(false);
		MainFrame.view_Navigation.button_ScratchPanel
				.setContentAreaFilled(false);
		MainFrame.view_Navigation.button_ScratchPanel.setBorderPainted(false);
		MainFrame.view_Navigation.button_ScratchPanel.setOpaque(false);
		MainFrame.view_Navigation.button_ScratchPanel.repaint();
		scratchWindowMounted = false;
		resize();
		revalidate();
		UIDelegate.SectionPaneHook.focusRow(UIDelegate.getDisplayedVerse()
				.getVerseNumber() - 1);
	}

	protected void unmountBookmarkPane()
	{
		if (!bookmarksWindowMounted) return;
		remove(getBookmarkPane());
		MainFrame.view_Navigation.button_Bookmarks.setSelected(false);
		MainFrame.view_Navigation.button_Bookmarks.setContentAreaFilled(false);
		MainFrame.view_Navigation.button_Bookmarks.setBorderPainted(false);
		MainFrame.view_Navigation.button_Bookmarks.setOpaque(false);
		MainFrame.view_Navigation.button_Bookmarks.repaint();
		bookmarksWindowMounted = false;
		resize();
		revalidate();
		UIDelegate.SectionPaneHook.focusRow(UIDelegate.getDisplayedVerse()
				.getVerseNumber() - 1);
	}

	protected void unmountSearchContainer()
	{
		if (!searchContainerMounted) return;
		remove(getSearchContainer());
		MainFrame.view_Navigation.button_Search.setSelected(false);
		MainFrame.view_Navigation.button_Search.setContentAreaFilled(false);
		MainFrame.view_Navigation.button_Search.setBorderPainted(false);
		MainFrame.view_Navigation.button_Search.setOpaque(false);
		MainFrame.view_Navigation.button_Search.repaint();
		searchContainerMounted = false;
		resize();
		revalidate();
		UIDelegate.SectionPaneHook.focusRow(UIDelegate.getDisplayedVerse()
				.getVerseNumber() - 1);
	}

	protected BookmarkPane getBookmarkPane()
	{
		if (pane_Bookmark == null)
		{
			pane_Bookmark = new BookmarkPane();
			if (new File("userdata").exists())
			{
				UIDelegate.BookmarkPaneHook.getBookmarkList()
						.loadBookmarks(
								new File("userdata" + File.separator
										+ "bookmarks.qbm"));
				UIDelegate.BookmarkPaneHook.refresh();
			}
		}
		return pane_Bookmark;
	}

	protected JPanel getSearchContainer()
	{
		if (container_Search == null)
		{
			container_Search = new JPanel();
			container_Search.setLayout(new BorderLayout());
			container_Search.setBorder(BorderFactory.createTitledBorder(
					BorderFactory
							.createMatteBorder(3, 3, 3, 3, Color.DARK_GRAY),
					ProjectLocalization.getString(
							ProjectConstants.CORE_PROJECT, "Search"), 0,
					TitledBorder.BELOW_TOP, FontLibrary.get(
							FontLibrary.DROID_SANS, Font.BOLD, 12),
					Color.DARK_GRAY));
			container_Search.add(getSearchPanel(), BorderLayout.CENTER);
			container_Search.add(getSearchFilterPanel(), BorderLayout.SOUTH);
		}
		return container_Search;

	}

	protected SearchConfigurationPanel getSearchFilterPanel()
	{
		if (panel_SearchConfiguration == null)
		{
			panel_SearchConfiguration = new SearchConfigurationPanel();
		}
		return panel_SearchConfiguration;
	}

	protected SearchPanel getSearchPanel()
	{
		if (panel_Search == null)
		{
			panel_Search = new SearchPanel();
		}
		return panel_Search;
	}

	protected ScratchPanel getScratchPanel()
	{
		if (panel_Scratch == null)
		{
			panel_Scratch = new ScratchPanel();
			panel_Scratch.setText(Main.runtimeSettings
					.getString("scratchpad.text"));
		}
		return panel_Scratch;
	}

	private void resize()
	{
		if (!isAWindowMounted()) setPreferredSize(new Dimension(0, 0));

		else
		{
			Dimension d = MainFrame.view_Browser.getPreferredSize();
			setPreferredSize(new Dimension(Math.max(560, d.width / 3), d.height));
		}
	}

}
